package com.yehuishou.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.Guestbook;
import com.yehuishou.pojo.Itempl;
import com.yehuishou.pojo.User;
import com.yehuishou.service.GuestbookService;

@Controller
public class GuestbookController {

	@Autowired
	private GuestbookService guestService;

	//查询回收评论
	@ResponseBody
	@RequestMapping("pingluen")
	public List<List<Guestbook>> find() {
		List<Guestbook> list = guestService.findAll();
		for (Guestbook gb : list) {
			gb.setPhone(gb.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
		}
		List<List<Guestbook>> lists = new ArrayList<>();
		int j = 0;
		for (int i = 0; i < list.size(); i++) {
			if (i != 0 && i % 4 == 0) {
				List<Guestbook> lis = new ArrayList<>();
				lists.add(lis);
				j++;
			}
			if (i == 0) {
				List<Guestbook> lis = new ArrayList<>();
				lists.add(lis);
			}
			lists.get(j).add(list.get(i));
		}
		return lists;
	}
	
	//添加回收评价
	@ResponseBody
	@RequestMapping("backpingjia")
	public Boolean addGPL(Guestbook gb , HttpSession session) {
		User user = (User) session.getAttribute("user");
		gb.setUid(user.getUid());
		gb.setGtime(new Date());
		guestService.addGPL(gb);
		return true;
	}
	
	
	
	//添加出售评价
	@ResponseBody
	@RequestMapping("sellpingjia")
	public Boolean addItemP(Itempl ipl , HttpSession session) {
		User user = (User) session.getAttribute("user");
		ipl.setUid(user.getUid());
		ipl.setTime(new Date());
		guestService.addItemP(ipl);
		return true;
	}
	
	//查询出售评价@
	@ResponseBody
	@RequestMapping("plitem")
	public PageInfo<Itempl> findIPL(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize,Integer i_id,Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<Itempl> list = guestService.findIPL(i_id);
		for (Itempl itempl : list) {
			itempl.setPhone(itempl.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			itempl.setDate(sdf.format(itempl.getTime()));
		}
		PageInfo<Itempl> page=new PageInfo<Itempl>(list);
		
		return page;
	}
	
}
